<?php

/**
 * 功 能：文件上传类 支持文件夹自动分组保存;
 * 创建类：参数(文件域,文件原名,文件大小);
 * $myupload = new upFileClass($upFile,$upFileName,$upFileSize);
 * $myupload->savefile(); # 保存方法 并返回保存路径附带文件名;
 * @ echo makeBuild($buildFile,$newFile,$fileWidth);
 * 生成指定文件的缩略图;
 * $myupload->makeBuild("images/a.jpg","news/b.jpg","100");
 */
class Library_Upload_Image
{
    var $upFile, $upFileName, $upFileSize;
    var $newUpFileName;             // 上传后的文件名称 ;
    var $fleth, $fileExtent;        // 文件扩展名(类型) ;
    var $path;                        // 文件保存路径(多级) upFiles/2008-01/08/;
    var $fileName;                    // 文件(带路径) ;
    var $maxSize, $fileType;        // 允许上传文件的大小 允许上传文件的类型 ;
    var $buildFile, $newFile, $fileWidth, $fileHeight, $rate;

    /**
     * 实例化上传图片
     * @param     $upFile       文件路径
     * @param     $upFileName   文件名
     * @param int $maxSize      允许上传文件的最大值
     */
    public function __construct($upFile, $upFileName, $savePath,$fileTypes, $maxSize = 0)
    {
        $this->upFile = $upFile;
        $this->fileExtent = array_pop(explode('.', $upFile));
        $this->upFileSize = @filesize($upfile);
        $this->newUpFileName = $this->createNewFilcename($upFileName);
        $this->path = rtrim($savePath,'/') . '/' . date('Y') . date('m') . "/" . date('d');
        $this->fileName = $this->path . "/" . $this->newUpFileName;
        $this->maxSize = $maxSize;      // 文件大小
        $this->fileType = $fileTypes;   // 允许上传的文件类型
    }

    /**
     * 创建新文件名
     *
     * @param $fileName
     *
     * @return string
     */
    public function createNewFilcename($fileName)
    {
        $this->fleth = explode(".", $fileName);
        return $tmpstr = ($this->fleth[0] ? $this->fleth[0] : date('YmdHis')) . "." . $this->fileExtent; # 创建新文件名;
    }

    /**
     * 检测文件类型是否正确
     */
    public function chkFileExtent()
    {
        $iwTrue = 0;
        for ($i = 0; $i < count($this->fileType); $i++) {
            if ($this->fileExtent == $this->fileType[$i]) {
                $iwTrue = (int)$iwTrue + 1;
            }
        }
        if ($iwTrue == 0) {
            $this->msg(1);
        }
    }

    /**
     * 提示错误信息并终止操作
     *
     * @param $Error
     */
    public function msg($Error)
    {
        return $Error;
    }

    /**
     * 保存文件
     * @return string
     */
    public function saveFile()
    {
        $this->chkFileExtent();
        $size = $this->chkFileSize();
        $this->createFolder($this->path);
        return $this->chkSaveFile();
    }

    /**
     * 检测上传结果是否成功
     * @return string
     */
    public function chkSaveFile()
    {
        $copymsg = copy($this->upFile, $this->fileName);
        if ($copymsg) {
            return $this->fileName;
        } else {
            $this->msg(2);
        }
    }

    /**
     * 递归创建文件夹
     *
     * @param $dir
     */
    public function createFolder($dir)
    {
        return is_dir($dir) or ($this->createFolder(dirname($dir)) and mkdir($dir, 0777));
    }

    /**
     * 检测文件大小
     */
    public function chkFileSize()
    {
        if ($this->maxSize && $this->upFileSize > $this->maxSize) {
            $this->msg(3);
        }
    }

    /**
     * 函数：生成缩略图
     * makeBuild("images/a.jpg","news/b.jpg","100");
     *
     * @param     $buildFile    原图 带路径
     * @param     $newFile      生成的缩略图 带路径  (此处默认为空值，及在原来的图片路径上进行更新)
     * @param     $fileWidth    缩略图宽度值
     * @param int $fileHeight   缩略图高度值 (默认为宽度的比例值)
     * @param int $rate         缩略图象品质
     *
     * @return bool
     */
    public function makeBuild($buildFile, $newFile, $fileWidth, $fileHeight = 0, $rate = 100)
    {
        if (!is_file($buildFile)) {
            $this->msg(4);
            return false;
        }
        $data = getimagesize($buildFile);
        switch ($data[2]) {
            case 1:
                $im = @imagecreatefromgif($buildFile);
                break;
            case 2:
                $im = @imagecreatefromjpeg($buildFile);
                break;
            case 3:
                $im = @imagecreatefrompng($buildFile);
                break;
        }
        if (!$im) {
            return false;
        } else {
            $srcW = imagesx($im); // 取得原图宽度;
            $srcH = imagesy($im); // 取得原图高度;
            $dstX = 0;
            $dstY = 0;

            if ($fileHeight == 0) {
                $fileHeight = $fileWidth / $srcW * $srcH;
            }

            if ($srcW * $fileHeight > $srcH * $fileWidth) {
                $ffileHeight = round($srcH * $fileWidth / $srcW);
                $dstY = floor(($fileHeight - $ffileHeight) / 2);
                $ffileWidth = $fileWidth;
            } else {
                $ffileWidth = round($srcW * $fileHeight / $srcH);
                $dstX = floor(($fileWidth - $ffileWidth) / 2);
                $ffileHeight = $fileHeight;
            }
            $ni = imagecreatetruecolor($fileWidth, $fileHeight);
            $dstX = ($dstX < 0) ? 0 : $dstX;
            $dstY = ($dstX < 0) ? 0 : $dstY;
            $dstX = ($dstX > ($fileWidth / 2)) ? floor($fileWidth / 2) : $dstX;
            $dstY = ($dstY > ($fileHeight / 2)) ? floor($fileHeight / s) : $dstY;
            imagecopyresized($ni, $im, $dstX, $dstY, 0, 0, $ffileWidth, $ffileHeight, $srcW, $srcH);
            $newFile = $newFile ? $newFile : preg_replace("/(\.jpg|\.gif|\.png|.jpeg)/","_".$fileWidth."$1",$buildFile);
            imagejpeg($ni, $newFile, $rate); // 生成缩略图;
            imagedestroy($im);               // imagedestroy(resource) 释放image关联的内存
            return $newFile;
        }
    }

}